package com.lmm.cg.zjgcg.jq.common;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.File;

public class ImageConvert
{
    private int width;
    public int getWidth()
    {
        return width;
    }
    private int height;
    public int getHeight()
    {
        return height;
    }
    public ImageConvert()
    {
        width = 0;
        height = 0;
    }

    private boolean PixelIsBlack(int color, int gray_threshold) {
		int red = ((color & 0x00FF0000) >> 16);
		int green = ((color & 0x0000FF00) >> 8);
		int blue = color & 0x000000FF;
		int grey = (int) ((float) red * 0.299 + (float) green * 0.587 + (float) blue * 0.114);
		return (grey < gray_threshold);
	}  


    public byte[] CovertImageVertical(Bitmap bitmap, int gray_threshold, int column_dots)
    {
        width = bitmap.getWidth();
        height = bitmap.getHeight();

        int count = (height - 1) / column_dots + 1;//???????С??
        int column_bytes = column_dots/8;
        byte[] data = new byte[width * column_bytes * count];

        int index = 0;

        int sx = 0, sy = 0;                                                                          //λ???x??y???????
        for (int i = 0; i < count; i++)                                                     //8???????й?????С?У????λ????LengthColumn??С?У?
        {
            for (int j = 0; j < width; j++)                                     //?С?е?λ???????BmpWidth?У????BmpWidth????????
            {
                sx = j;                                                                             //λ??????????x?????????x??
                for (int k = 0; k < 8; k++)                                                        //k??С????8???????е????У?
                {
                    sy = (i << 3) + k;                                                              //λ??????????y?????(С??????8)+k;
                    if (sy >= height)                                            //???λ??????????y??????????λ????(λ???????????8????????)????????????????ж??
                    {
                        continue;
                    }
                    else
                    {
                        if (PixelIsBlack(bitmap.getPixel(sx, sy), gray_threshold))                    //?ж??????????????
                        {
                            data[index] |= (byte)(0x01 << (7 - k));      //??????????????????????????????1??
                        }
                    }
                }
                index++;                                                      //?С?е??????????ж?????λ????????????1??
            }
        }
        return data;
    }

    /// <summary>
    /// ?????????????????
    /// </summary>
    /// <param name="image_path">???·??@param
    /// <param name="gray_threshold">???@param
    /// <returns>null????????????????????</returns>
    public byte[] CovertImageVertical(String image_path, int gray_threshold)
    {
    	if(new File(image_path).exists())
        {
    		Bitmap bitmap = BitmapFactory.decodeFile(image_path);
    	    byte[] data = CovertImageVertical(bitmap, gray_threshold,8);
    	    if (data == null)
    	    	Log.e("JQ","?????????");
            return data;
        }
    	else
    	{
    		Log.e("JQ","???·??????:" + image_path);
    		return null;
    	}
    }

    /// <summary>
    /// ????????????????
    /// </summary>
    /// <param name="bitmap">???@param
    /// <param name="gray_threshold">???@param
    /// <returns>null????????????????????</returns>
    public byte[] CovertImageHorizontal(Bitmap bitmap, int gray_threshold)
    {
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        int BytesPerLine = (width - 1) / 8 + 1;

        byte[] data = new byte[BytesPerLine * height];

        int index = 0;

        //????λ?????????????????λ???????????
        int x = 0, y = 0;                                                                          //λ???x??y???????
        for (int i = 0; i < height; i++)                                        //????ж???????У?????ж?BmpHeight??
        {
            for (int j = 0; j < BytesPerLine; j++)                                                    //??????LengthRow??????????
            {
                for (int k = 0; k < 8; k++)                                                        //????????8??????1??1λ??
                {
                    x = (j << 3) + k;                                                              //x??????????????8+???????λk??
                    y = i;                                                                         //????У?
                    if (x >= width)                                             //???λ??????????y??????????λ????(λ???????????8????????)????????????????ж??
                    {
                        continue;
                    }
                    else
                    {
                        if (PixelIsBlack(bitmap.getPixel(x, y), gray_threshold))
                        {
                            data[index] |= (byte)(0x01 << k);
                        }
                    }
                }
                index++;                                                       //?????????8???ж?????λ????????????1??
            }
            x = 0;
        }

        return data;
    }

    /// <summary>
    /// ????????????????
    /// </summary>
    /// <param name="image_path">???·??@param
    /// <param name="gray_threshold">???@param
    /// <returns>null????????????????????</returns>
    public byte[] CovertImageHorizontal(String image_path, int gray_threshold)
    {
    	if(new File(image_path).exists())
        {
    		Bitmap bitmap = BitmapFactory.decodeFile(image_path);
    		byte[] data = CovertImageHorizontal(bitmap, gray_threshold);
    	    if (data == null)
    	    	Log.e("JQ","?????????");
            return data;
        }
    	else
    	{
    		Log.e("JQ","???·??????:" + image_path);
    		return null;
    	}   
    }
}